package com.burningtower.controller;


import com.burningtower.common.Result;
import com.burningtower.entity.Organization;
import com.burningtower.service.IOrganizationService;
import com.burningtower.vo.OrganizationVo;
import com.burningtower.vo.TreeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author zhe
 * @since 2017-11-17
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController {
    @Autowired
    private IOrganizationService service;

    @RequestMapping("tree")
    public List<TreeVo> tree(){
        List<Organization> organizations = service.selectList(null);
        List<TreeVo> treeVos=new ArrayList<>();
        for(Organization organization:organizations){
            treeVos.add(service.toVo(organization));
        }
        return treeVos;
    }
    @RequestMapping("treeGrid")
    public List<OrganizationVo> treeGrid(){
        List<Organization> organizations = service.selectList(null);
        List<OrganizationVo> organizationVos=new ArrayList<>();
        for(Organization o:organizations){
            OrganizationVo organizationVo = new OrganizationVo();
            BeanUtils.copyProperties(o,organizationVo);
            organizationVo.setIconCls(o.getIcon());
            organizationVos.add(organizationVo);
        }
        return organizationVos;
    }
    @RequestMapping("delete")
    public Result delete(Long id){
        service.deleteById(id);
        return new Result("删除成功！");
    }
    @RequestMapping("edit")
    public Result edit(Organization organization){
        service.updateById(organization);
        return new Result("编辑成功！");
    }
    @RequestMapping("add")
    public Result add(Organization organization){
        organization.setCreateTime(new Date());
        service.insert(organization);
        return new Result("添加成功");
    }
}

